<?php
include("../Common/Core_brain.php");

if(!$isLogin)exit('{"code":-1,"msg":"未登录"}');

$type = isset($_POST['type']) ? $_POST['type'] : '';
if(empty($type))exit('{"code":-1,"msg":"参数错误"}');

if(!isset($_FILES['file']))exit('{"code":-1,"msg":"请选择文件"}');

$file = $_FILES['file'];
if($file['error'] > 0)exit('{"code":-1,"msg":"文件上传失败"}');

// 检查文件类型
$allow_types = array('image/jpeg', 'image/png', 'image/gif');
if(!in_array($file['type'], $allow_types))exit('{"code":-1,"msg":"只支持jpg、png、gif格式"}');

// 检查文件大小
if($file['size'] > 5*1024*1024)exit('{"code":-1,"msg":"文件大小不能超过5MB"}');

// 生成文件名
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = date('YmdHis').rand(1000,9999).'.'.$ext;

// 创建上传目录
$upload_dir = '../upload/'.$type.'/';
if(!is_dir($upload_dir))mkdir($upload_dir, 0777, true);

// 移动文件
if(move_uploaded_file($file['tmp_name'], $upload_dir.$filename)){
    $url = '/upload/'.$type.'/'.$filename;
    exit('{"code":1,"msg":"上传成功","url":"'.$url.'"}');
}else{
    exit('{"code":-1,"msg":"文件保存失败"}');
} 